import { BULLET_SPEED } from '../../../config/play_def';
import bullet from '../../base/bullet'
cc.Class({
    extends: bullet,

    properties: {
    },
    initInfo(param){
        this.script_name = `missile`    // 脚本名赋值 必填！
        this.attack_fly = param.attack_fly
        this.order_line = param.attack_line
        this.range = param.range
        this._super(param)
    },
    turnDir(){
        let t = this.dir.signAngle(cc.v2(1,0));
        if(t){
            var degree = t / Math.PI * 180 + 90;
            while (Math.abs(degree) > 360) {
                if(degree < 0){
                    degree += 360
                }else{
                    degree -= 360
                }
            }
            if(degree < 0){
                degree = 360 + degree
            }    
            this.node.angle = -degree
        }
    },
    // 范围攻击
    rangeHit(monster){
        for(let i = gg.boidsManager.boids_list.length - 1;i >= 0;i--){
            if(!gg.boidsManager.boids_list[i]){
              continue
            }
            if(!gg.boidsManager.boids_list[i].canAttack()){
                continue
            }
            let boidsWorldPos = gg.boidsManager.boids_list[i].getWorldPos()
            let distance = monster.getWorldPos().sub(boidsWorldPos).mag()
            if(distance < this.range){
                let damege = this.damege
                if(this.range * 0.8 > distance && distance > this.range * 0.3){
                    damege = parseInt(damege*0.8)
                }else if(distance >= this.range * 0.8 ){
                    damege = parseInt(damege*0.5)
                }
                this.happenEffect(gg.boidsManager.boids_list[i])
                this.playHit(gg.boidsManager.boids_list[i])
            }
        }
    },
    // 攻击
    attack(monster){
        this._super(monster)  // 伤害变更
        if(this.collision_destory){
            this.first_collision = true
            this.move = false
            this.rangeHit(monster)
        }else{
            this.rangeHit(monster)
        }
    },
    fly(dt){
        this._super(dt)
        let target = this.dir.mul(this.speed* BULLET_SPEED *dt)
        this.node.position = target.add(this.node.position)
    }
});
